﻿namespace dPet.Domain.Events.Handlers
{
	using dPet.Domain.Models;
	using dPet.Domain.Repositories;
	using Hangerd.Event;

	public class InventoryCheckedEventHandler : IDomainEventHandler<InventoryCheckedEvent>
	{
		private readonly IInventoryAlarmItemRepository _inventoryAlarmItemRepository;

		public InventoryCheckedEventHandler(IInventoryAlarmItemRepository inventoryAlarmItemRepository)
		{
			_inventoryAlarmItemRepository = inventoryAlarmItemRepository;
		}

		public void Handle(InventoryCheckedEvent e)
		{
			var product = e.Source as Product;

			//删除旧的告警项
			_inventoryAlarmItemRepository.RemoveItemsByProductAndAlarmType(product, e.AlarmType);

			//添加新的告警项
			if (e.AlarmItem != null)
			{
				_inventoryAlarmItemRepository.Add(e.AlarmItem, false);
			}
		}
	}
}
